/**
 * @Title: PointStatisticsController
 * @Package cn.com.yiqi.wsc.portal.controller.point
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @author 会员系统组-胥钰淋
 * @date 2017/4/8 12:01
 * @version V1.0
 */
package cn.com.yiqi.wsc.portal.controller.point;

import cn.com.yiqi.wsc.Ro.PointDetailVO;
import cn.com.yiqi.wsc.Ro.PortalPointVO;
import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.dao.point.PointRecordDao;
import cn.com.yiqi.wsc.dao.point.criteria.PointCriteria;
import cn.com.yiqi.wsc.dao.point.entity.PointRecordEntity;
import cn.com.yiqi.wsc.enumeration.PointBizType;
import cn.com.yiqi.wsc.enumeration.PointRecordStatus;
import cn.com.yiqi.wsc.enumeration.PointStatus;
import cn.com.yiqi.wsc.portal.controller.BaseController;
import cn.com.yiqi.wsc.service.customer.PortalCutomerService;
import cn.com.yiqi.wsc.service.outputExcel.OutPutService;
import cn.com.yiqi.wsc.service.point.PointService;
import cn.com.yiqi.wsc.service.point.PortalPointService;
import cn.com.yiqi.wsc.service.point.dataobject.QueryPointDO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *
 *@ClassName: PointStatisticsController
 * @Description: TODO
 * @author 会员系统组-胥钰淋
 * @date 2017/4/8 12:01
 *
 */
@RequestMapping("/pointStatistics")
@Controller
public class PointStatisticsController extends BaseController {

    @Autowired
    private PortalCutomerService portalCutomerService;

    @Autowired
    private PointService pointService;

    @Autowired
    private OutPutService outPutService;

    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("point/index");
        PointCriteria criteria = new PointCriteria();
        Long sendTotal = pointService.getSendTotal(criteria);
        Long usedTotal = pointService.getUsedTotal(criteria);
        this.getUserData(mv);
        mv.addObject("sendTotal", sendTotal);
        mv.addObject("usedTotal", usedTotal);
        return mv;
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Result list (@RequestBody PointCriteria criteria) {
        Result result = new Result();
        if (null == criteria) {
            return ResultUtil.fail(result, "参数错误！");
        }
//        criteria.setBizTypeList(Arrays.asList(new Integer[]{
//                PointBizType.deal_get.getValue(),
//                PointBizType.lottery_get.getValue(),
//                PointBizType.sys_send.getValue(),
//                PointBizType.spread_sale_get.getValue(),
//                PointBizType.sign_get.getValue()
//        }));
        if (criteria.getOperateType() == -1) {
            criteria.setOperateType(null);
        }
        result = portalCutomerService.findCustomerPointDetail(criteria);

        Long sendTotal = pointService.getSendTotal(criteria);
        if (null == sendTotal) {
            sendTotal = 0l;
        }
        Long usedTotal = pointService.getUsedTotal(criteria);
        if (null == usedTotal) {
            usedTotal = 0l;
        }

        result.putData("searchSendTotal", sendTotal);
        result.putData("searchUsedTotal", usedTotal);
        return result;
    }



    @RequestMapping("/outPut")
    public void outPut(@RequestParam(name = "keyword",required = false) String keyword,
                       @RequestParam(name = "bizTypeList",required = false) Integer[] bizTypeList,
                       @RequestParam(name = "operateType",required = false) Integer operateType,
                       @RequestParam(name = "from",required = false)  @DateTimeFormat(pattern = "yyyy-MM-dd") Date from,
                       @RequestParam(name = "to",required = false)  @DateTimeFormat(pattern = "yyyy-MM-dd")  Date to,
                       HttpServletRequest request, HttpServletResponse response) {
        PointCriteria criteria = new PointCriteria();
        criteria.setKeyword(keyword);
        if(bizTypeList!=null){
            criteria.setBizTypeList(Arrays.asList(bizTypeList));
        }
        criteria.setOperateType(operateType);
        criteria.setFrom(from);
        criteria.setTo(to);
        criteria.setPageSize(500000);
        List<PointDetailVO> list = (List<PointDetailVO>)portalCutomerService.findCustomerPointDetail(criteria).getData().get("pointList");
        if(list !=null){
            for(PointDetailVO pd : list){
                pd.setPointChangDesc("-"+pd.getPoint());
                if(pd.getOperateType()==1){
                    pd.setPointChangDesc("+"+pd.getPoint());
                }

            }

        }

        String title = "平台积分管理";
        String[] hearders = new String[]{"客户昵称", "手机号", "来源", "积分变化","时间","备注"};//表头数组
        String[] fields = new String[]{"customerNick", "tel", "typeName", "pointChangDesc","insertTime","bizDesc"};//People对象属性数组
        String creater = userService.getCurrentUser().getRealName();
        try {
            outPutService.outPutExcel(list, title, hearders, fields, creater, request, response);
        } catch (Exception e) {
            e.printStackTrace();

        }

    }



}
